$namespace("gdgs.ndp.util");

gdgs.ndp.util.resetPxh = function(draggingInfo, pxhName, no){
	return this.resetPxhByEntity(draggingInfo.get("object"), pxhName, no);
};

gdgs.ndp.util.resetPxhByEntity = function(dragEntity, pxhName, no){
	var previousEntity = dragEntity.getPrevious();
	var nextEntity = dragEntity.getNext();
	if(!previousEntity && !nextEntity){
		return false;
	}
	if(!pxhName){
		pxhName = "pxh";
	}
	if(!no){
		no = 20;
	}
	var nno = Math.round(no/2);
	var pxh = dragEntity.get(pxhName);
	if(!pxh){
		pxh = 0;
	}
	if(!nextEntity){//drag to last
		var prevPxh = previousEntity.get(pxhName);
		if(!prevPxh){
			prevPxh = 0;
		}
		if(pxh > prevPxh){
			return false;
		}
		dragEntity.set(pxhName, prevPxh +no);
	}else{// drag to between two entity
		var prevPxh = 0;
		if(previousEntity){
			prevPxh = previousEntity.get(pxhName);
			if(!prevPxh){
				prevPxh = 0;
			}
		}
		var nextPxh = nextEntity.get(pxhName);
		if(!nextPxh){
			nextPxh = 0;
		}
		if(pxh > prevPxh && pxh < nextPxh){
			return false;
		}
		pxh = Math.round((prevPxh+nextPxh)/2);
		if(pxh >= nextPxh){
			if(nextPxh<=0){
				nextPxh = nno;
			}
			nextPxh = nextPxh + nno;
			pxh = Math.round((prevPxh+nextPxh)/2);
			nextEntity.set(pxhName, nextPxh);
			nextEntity = nextEntity.getNext();
			while(nextEntity){
				var nnPxh = nextEntity.get(pxhName);
				if(nnPxh && nextPxh < nnPxh){
					break;
				}
				nextPxh = nextPxh + nno;
				nextEntity.set(pxhName, nextPxh);
				nextEntity = nextEntity.getNext();
			}
		}
		dragEntity.set(pxhName, pxh);
	}
	return true;
};